package com.zq.widget.inverted.game.role.anim;

import android.animation.TypeEvaluator;
import android.graphics.PointF;

/**
 * Created by zhangqiang on 2018/3/28.
 */
public class BezierEvaluator implements TypeEvaluator<PointF> {

    private PointF control;

    public BezierEvaluator(PointF control) {
        this.control = control;
    }

    public BezierEvaluator(){

    }

    @Override
    public PointF evaluate(float t, PointF startValue, PointF endValue) {


        if (control == null) {
            float xDistance = endValue.x - startValue.x;
            float yDistance = endValue.y - startValue.y;
            float cx = (float) (xDistance * Math.random() + startValue.x);
            float cy = (float) (yDistance * Math.random() + startValue.y);
            control = new PointF(cx, cy);
        }

        float oneMinusT = 1 - t;

        float x = oneMinusT * oneMinusT * startValue.x +
                2 * oneMinusT * t * control.x +
                t * t * endValue.x;

        float y = oneMinusT * oneMinusT * startValue.y +
                2 * oneMinusT * t * control.y +
                t * t * endValue.y;
//        float x = startValue.x + t* (endValue.x - startValue.x);
//        float y = startValue.y + t * (endValue.y - startValue.y);
        return new PointF(x, y);
    }
}
